package xy.donate.android.ui.fragment

import android.content.Intent
import android.widget.Button
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.room.Room
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.hjq.base.BaseDialog
import kotlinx.android.synthetic.main.fragment_help.*
import xy.donate.android.R
import xy.donate.android.common.MyFragment
import xy.donate.android.constant.Constant
import xy.donate.android.db.HandsUpDB
import xy.donate.android.db.HandsUpDao
import xy.donate.android.entity.HandsUpEntity
import xy.donate.android.helper.MmkvHelper
import xy.donate.android.ui.activity.*

class HelpFragment : MyFragment<HomeActivity>() {

    private lateinit var adapter: BaseQuickAdapter<String, BaseViewHolder>

    private lateinit var db:HandsUpDao

    override fun getLayoutId(): Int {
        return R.layout.fragment_help
    }

    override fun initView() {
        recycler_view.layoutManager = LinearLayoutManager(requireContext(),LinearLayoutManager.HORIZONTAL,false)
        recycler_view.setHasFixedSize(true)
        adapter = object : BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_help_to_img, ArrayList()) {
            override fun convert(holder: BaseViewHolder, item: String) {
                Glide.with(context.applicationContext)
                        .load(item)
                        .into(holder.getView(R.id.iv_item_img))
            }
        }
        adapter.addChildClickViewIds(R.id.iv_item_img, R.id.iv_delete)
        adapter.setOnItemChildClickListener { _, view, position ->
            when (view.id) {
                R.id.iv_item_img -> {
                    ImagePreviewActivity.start(requireContext(),adapter.data)
                }
                R.id.iv_delete -> {
                    adapter.removeAt(position)
                }
            }
        }
        recycler_view.adapter = adapter
        tv_upload_img.setOnClickListener {
            ImageSelectActivity.start(attachActivity) { data: List<String> ->
                if (adapter.data.isNotEmpty()) {
                    adapter.data.addAll(data)
                    adapter.notifyDataSetChanged()
                } else {
                    adapter.setList(data)
                }
            }
        }

        tv_ui_confirm.setOnClickListener {
            val name = et_name.text.toString()
            val phone = et_phone.text.toString()
            val hardSm = et_qk.text.toString()
            val help = et_mp.text.toString()
            val list = adapter.data
            if (name.isEmpty()){
                toast("您的名字不能为空")
                return@setOnClickListener
            }
            if (phone.isEmpty()){
                toast("您的手机不能为空")
                return@setOnClickListener
            }
            if (hardSm.isEmpty()){
                toast("您的困难说明不能为空")
                return@setOnClickListener
            }
            if (help.isEmpty()){
                toast("您的帮助说明不能为空")
                return@setOnClickListener
            }
            if (list.isEmpty()){
                toast("您的凭证资料不能为空")
                return@setOnClickListener
            }
            val entity = HandsUpEntity()
            entity.recordID = Constant.getOrderNo()
            entity.userID = MmkvHelper.getInstance().mmkv.decodeString(Constant.USER_ID_NUM)
            entity.name = name
            entity.phone = phone
            entity.hardText = hardSm
            entity.helpText = help
            entity.images = list
            db.insert(entity)
            BaseDialog.Builder<BaseDialog.Builder<*>>(requireActivity())
                    .setContentView(R.layout.custom_dialog)
                    .setAnimStyle(BaseDialog.ANIM_SCALE) //.setText(id, "我是预设置的文本")
                    .setOnClickListener(R.id.btn_dialog_custom_ok, BaseDialog.OnClickListener<Button?> { dialog: BaseDialog, view: Button? -> dialog.dismiss() })
                    .setText(R.id.title,"申请成功")
                    .setText(R.id.text,"工作人员将在三个工作日之内联系您")
                    .addOnDismissListener {
                        val intent = Intent(requireContext(), RecordDetailActivity::class.java)
                        intent.putExtra("recordID", entity.recordID)
                        startActivity(intent)
                        et_name.setText("")
                        et_phone.setText("")
                        et_qk.setText("")
                        et_mp.setText("")
                        adapter.data.clear()
                        adapter.notifyDataSetChanged()
                    }
                    .show()
        }

        db = Room.databaseBuilder(requireContext(),HandsUpDB::class.java,Constant.RECORD_TABLE)
                .allowMainThreadQueries()
                .build().handsDao
    }

    override fun initData() {

    }
}